<?php
class Admin_Model_Articles extends Core_Database
{
    /**
     * Lay danh sach tat ca cac bai viet
     * @param: void
     * @return : array(): neu co it nhat mot bai viet  null: neu khong co bai viet nao
     * 
     * */
    public function getArticles()
    {
     $sql = 'select * from articles order by article_id ASC';
     $sth = $this->_db->prepare($sql);
     $status = $sth->execute();
     if (!$status)
          return null;
     $result = $sth->fetchAll();
     if(count($result)>0)
        return $result;
     else
        return null;   
    }
    /**
     * Them mot bai viet moi
     * @param: title content group_index
     * @return: bool  true: thanh cong, false : that bai
     * 
     * */
    public function insertArticle($title,$content,$group_index)
    {
        $sql = 'insert into articles(title,content,group_index) values(:title,:content,:group_index)';
    	$sth = $this->_db->prepare($sql);
    	$sth->bindParam(':title', $title, PDO::PARAM_STR);
    	$sth->bindParam(':content', $content, PDO::PARAM_STR);
    	$sth->bindParam(':group_index', $group_index, PDO::PARAM_INT);
    	return $sth->execute();
    } 
    
    /**
     * Lay mot bai viet
     * @param : article_id
     * @return : array : neu ton tai  null : neu khong ton tai
     * */
    public function getArticle($article_id)
    {
        $sql = 'select * from articles where article_id = :article_id';
        $sth = $this->_db->prepare($sql);
        $sth->bindParam(':article_id',$article_id,PDO::PARAM_INT);
        $status = $sth->execute();
        if (!$status)
            return null;
        $result = $sth->fetchAll();
        if(count($result)>0)
           return $result;
        else
           return null;  
    }
    
    /**
     * Cap nhat bai viet
     * @param : article_id  title content group_index
     * @return bool true : thanh cong , false: that bai
     * */
     public function updateArticle($article_id,$title,$content,$group_index)
     {
        $sql = 'update articles set title = :title, content = :content, group_index = :group_index where article_id = :article_id';
    	$sth = $this->_db->prepare($sql);
    	$sth->bindParam(':article_id',$article_id,PDO::PARAM_INT);
  	    $sth->bindParam(':title', $title, PDO::PARAM_STR);
        $sth->bindParam(':content', $content, PDO::PARAM_STR);
        $sth->bindParam(':group_index', $group_index, PDO::PARAM_INT);
    	return $sth->execute();
     }
     
     /**
      * Xoa mot bai viet
      * @param :article_id
      * @return bool  true: thanh cong false: that bai
      * */
      public function deleteArticle($article_id)
      {
        $sql = 'delete from articles where article_id = :article_id';
        $sth = $this->_db->prepare($sql);
        $sth->bindParam(':article_id',$article_id,PDO::PARAM_INT);
        return $sth->execute();
      } 
}